<template>
	<el-drawer v-model="visible" :title="!dataForm.id ? '新增' : '修改'">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
	    		<el-form-item v-show="!visible" label="客户编码" prop="clientId">
					<el-input v-model="dataForm.id" placeholder=""></el-input>
				</el-form-item>
				<el-form-item label="客户代码" prop="clientCode">
					<el-input v-model="dataForm.clientCode" placeholder="客户代码"></el-input>
				</el-form-item>
				<el-form-item label="客户实体名称" prop="clientName">
					<el-input v-model="dataForm.clientName" placeholder="客户实体名称"></el-input>
				</el-form-item>
				<el-form-item label="客户实体名称-英文" prop="clientNameEn">
					<el-input v-model="dataForm.clientNameEn" placeholder="客户实体名称-英文"></el-input>
				</el-form-item>
				<el-form-item label="客户税号抬头" prop="dutyParagraph">
					<el-input v-model="dataForm.dutyParagraph" placeholder="客户税号抬头"></el-input>
				</el-form-item>
				<el-form-item label="客户国家" prop="clientNation">
					<el-input v-model="dataForm.clientNation" placeholder="客户国家"></el-input>
				</el-form-item>
				<el-form-item label="客户国家简称" prop="clientNationAbbreviation">
					<el-input v-model="dataForm.clientNationAbbreviation" placeholder="客户国家简称"></el-input>
				</el-form-item>
				<!-- <el-form-item label="" prop="clientParentId">
					<el-input v-model="dataForm.clientParentId" placeholder=""></el-input>
				</el-form-item> -->
				<el-form-item label="地址" prop="address">
					<el-input v-model="dataForm.address" placeholder="地址"></el-input>
				</el-form-item>
				<el-form-item label="权利归属" prop="accountOwner">
					<el-input v-model="dataForm.accountOwner" placeholder="权利归属"></el-input>
				</el-form-item>
				<el-form-item label="开户行" prop="bank">
					<el-input v-model="dataForm.bank" placeholder="开户行"></el-input>
				</el-form-item>
				<!-- <el-form-item label="删除标识  0：正常   1：已删除" prop="deleted">
					<el-input v-model="dataForm.deleted" placeholder="删除标识  0：正常   1：已删除"></el-input>
				</el-form-item> -->
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useClientsApi, useClientsSubmitApi } from '@/api/erp/sys_clients'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	clientCode: '',
	clientName: '',
	clientNameEn: '',
	dutyParagraph: '',
	clientNation: '',
	clientNationAbbreviation: '',
	clientParentId: '',
	address: '',
	accountOwner: '',
	bank: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getClients(id)
	}
}


const getClients = (id: number) => {
	useClientsApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useClientsSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
